.TH std::ranges::views::keys,std::ranges::keys_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::views::keys,std::ranges::keys_view \- std::ranges::views::keys,std::ranges::keys_view

.SH Synopsis
   Defined in header <ranges>
   template< class R >                                   \fB(1)\fP \fI(since C++20)\fP
   using keys_view = ranges::elements_view<R, 0>;
   namespace views {

       inline constexpr auto keys = ranges::elements<0>; \fB(2)\fP \fI(since C++20)\fP

   }

   Takes a view of tuple-like values (e.g. std::tuple or std::pair), and produces a
   view with a value-type of the first element of the adapted view's value-type.

   1) An alias for ranges::elements_view<R, 0>.
   2) RangeAdaptorObject (and also RangeAdaptorClosureObject). The expression
   views::keys(e) is expression-equivalent to keys_view<views::all_t<decltype((e))>>{e}
   for any suitable subexpression e.

.SH Notes

   keys_view can be useful for extracting keys from associative containers, e.g.

 std::map<std::string, int> map{{"one", 1}, {"two", 2}};

 for (auto const& key : std::views::keys(map))
     std::cout << key << ' ';
 // prints: one two

.SH Example

   Displays values for each type of quark in particle physics.


// Run this code

 #include <iomanip>
 #include <iostream>
 #include <locale>
 #include <ranges>
 #include <string>
 #include <tuple>
 #include <vector>

 int main()
 {
     const std::vector<std::tuple<std::string, double, bool>> quark_mass_charge
     {
         // name, MeV/c², has positive electric-charge:
         {"up", 2.3, true}, {"down", 4.8, false},
         {"charm", 1275, true}, {"strange", 95, false},
         {"top", 173'210, true}, {"bottom", 4'180, false},
     };

     std::cout.imbue(std::locale("en_US.utf8"));
     std::cout << "Quark name:  │ ";
     for (std::string const& name : std::views::keys(quark_mass_charge))
         std::cout << std::setw(9) << name << " │ ";

     std::cout << "\\n" "Mass MeV/c²: │ ";
     for (const double mass : std::views::values(quark_mass_charge))
         std::cout << std::setw(9) << mass << " │ ";

     std::cout << "\\n" "E-charge:    │ ";
     for (const bool pos : std::views::elements<2>(quark_mass_charge))
         std::cout << std::setw(9) << (pos ? "+2/3" : "-1/3") << " │ ";
     std::cout << '\\n';
 }

.SH Output:

 Quark name:  │        up │      down │     charm │   strange │       top │    bottom │
 Mass MeV/c²: │       2.3 │       4.8 │     1,275 │        95 │   173,210 │     4,180 │
 E-charge:    │      +2/3 │      -1/3 │      +2/3 │      -1/3 │      +2/3 │      -1/3 │

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to            Behavior as published              Correct behavior
   LWG 3563 C++20      keys_view is unable to participate in CTAD  views::all_t removed
                       due to its use of views::all_t

.SH See also

   ranges::values_view   takes a view consisting of pair-like values and produces a
   views::values         view of the second elements of each pair
   (C++20)               \fI(class template)\fP (range adaptor object)
   ranges::elements_view takes a view consisting of tuple-like values and a number N
   views::elements       and produces a view of N^th element of each tuple
   (C++20)               \fI(class template)\fP (range adaptor object)
   slice                 BLAS-like slice of a valarray: starting index, length, stride
                         \fI(class)\fP
